<?php

namespace app\common\model\xiluhouse;

use think\Model;
use traits\model\SoftDelete;

class House extends Base
{

    use SoftDelete;

    // 表名
    protected $name = 'xiluhouse_house';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        // 'images_arr2',
        // 'images_arr',
        // 'house_tag_ids_text_arr',
        // 'building_descript_arr',
        // 'property_descript_arr',
        // 'images_arr2',
        // 'images_arr',
        // 'house_tag_ids_text_arr',
        // 'building_descript_arr',
        // 'property_descript_arr',
        // 'nearby_bus_json_arr',
        // 'nearby_subway_json_arr',
        // 'nearby_school_json_arr',
        // 'nearby_shop_json_arr',
        // 'nearby_hospital_json_arr',
        // 'rank_json_arr',

        'rank_json_text',
        'build_image_text',
        'image_text',
        'image_text2',
        'sale_state_text',
        'is_low_price_protect_text',
        'opentime_text',
        'deliverytime_text',
        'viewtime_text',
        'saletime_text',
        'collecttime_text',
        'recievetime_text',
        'status_text',
    ];

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }
    public function getMinTotalPriceAttr($value, $data)
    {
        return  $value ?: (isset($data['min_total_price']) ? $data['min_total_price'] : '');

    }
    public function getMaxTotalPriceAttr($value, $data)
    {
        return $value ?: (isset($data['max_total_price']) ? $data['max_total_price'] : '');

    }
    public function getMinSpaceAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['min_space']) ? $data['min_space'] : '');
        return $value;
    }
    public function getMaxSpaceAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['max_space']) ? $data['max_space'] : '');
        return intval($value);
    }

    // 获取是否点赞该评论
    public function getIsCollectAttr($value, $data)
    {
        $id = $data['id'];
        $user_id = 0;
        $request = \think\Request::instance();
        $token = $request->server('HTTP_TOKEN', $request->request('token', \think\Cookie::get('token')));
        if ($token) {
            $auth = \app\common\library\Auth::instance();
            $auth->init($token);
            if ($auth) {
                $user_id = $auth->id;
            }
        }
        if ($user_id) {
            $exist = HouseCollect::where('type', 1)->where('house_id', $id)->where('user_id', $user_id)->find();
            if ($exist) {
                return true;
            }
        }
        return false;
    }
    public function getRankJsonTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['rank_json']) ? $data['rank_json'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        $text = '';
        foreach ($arr as $k => $v) {
            $text = $v['rank_area'] . $v['rank_type_text'] . '排名第' . $v['rank_number'] . '名';
        }
        return $text;
    }
    public function getRankJsonArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['rank_json']) ? $data['rank_json'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        return $arr;
    }
    // 处理建筑信息
    public function getBuildingDescriptArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['building_descript']) ? $data['building_descript'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        return $arr;
    }
    // 处理物业信息
    public function getPropertyDescriptArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['property_descript']) ? $data['property_descript'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        return $arr;
    }
    // 处理信息
    public function getNearbyBusJsonArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['nearby_bus_json']) ? $data['nearby_bus_json'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        return $arr;
    }
    // 处理信息
    public function getNearbyHospitalJsonArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['nearby_hospital_json']) ? $data['nearby_hospital_json'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        return $arr;
    }
    // 处理信息
    public function getNearbySubwayJsonArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['nearby_subway_json']) ? $data['nearby_subway_json'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        return $arr;
    }
    // 处理信息
    public function getNearbySchoolJsonArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['nearby_school_json']) ? $data['nearby_school_json'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        return $arr;
    }
    // 处理信息
    public function getNearbyShopJsonArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['nearby_shop_json']) ? $data['nearby_shop_json'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        return $arr;
    }
    // 处理多图的情况
    public function getImagesArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['images']) ? $data['images'] : '');
        $images_arr = [];
        if ($value) {
            $arr = json_decode($value, true);
            if (is_array($arr)) {
                foreach ($arr as $now_images) {
                    if ($now_images['images']) {
                        $now_images_arr = explode(',', $now_images['images']);
                        foreach ($now_images_arr as $now_image_key => $now_image_value) {
                            $now_images_arr[$now_image_key] = cdnurl($now_image_value,true);
                        }
                        $images_arr = array_merge($images_arr, $now_images_arr);
                    }
                }
            }
        }
        return $images_arr;
    }
    // 处理多图的情况
    public function getImagesArr2Attr($value, $data)
    {
        $value = $value ? $value : (isset($data['images']) ? $data['images'] : '');
        $images_arr = [];
        if ($value) {
            $arr = json_decode($value, true);
            if (is_array($arr)) {
                foreach ($arr as $now_images) {
                    if ($now_images['images']) {
                        $now_images_arr = explode(',', $now_images['images']);
                        foreach ($now_images_arr as $now_image_key => $now_image_value) {
                            $now_images_arr[$now_image_key] = cdnurl($now_image_value,true);
                        }
                        $images_arr[] = ['name' => $now_images['name']??'', 'images_arr' => $now_images_arr];
                    }
                }
            }
        }
        return $images_arr;
    }
    public function getHouseTypeIdTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['house_type_id']) ? $data['house_type_id'] : '');
        $name = '';
        if ($value) {
            $name = HouseType::where('status', 'normal')->where('id', $value)->orderRaw('weigh desc,id desc')->value('name');
        }
        return $name;
    }
    public function getHouseTagIdsTextArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['house_tag_ids']) ? $data['house_tag_ids'] : '');
        $tag_name_arr = [];
        if ($value) {
            $tag_name_arr = HouseTag::where('status', 'normal')->where('id', 'in', $value)->orderRaw('weigh desc,id desc')->column('name');
        }
        return $tag_name_arr;
    }
    public function getHouseTagIdsText1Attr($value, $data)
    {
        $value = $value ? $value : (isset($data['house_tag_ids']) ? $data['house_tag_ids'] : '');
        $text = '';
        if ($value) {
            $data = HouseTag::where('status', 'normal')->where('id', 'in', $value)->where('is_left_top', 2)->orderRaw('is_left_top desc,weigh desc,id desc')->find();
            if ($data) {
                $text = $data['name'];
            }
        }
        return $text;
    }
    public function getHouseTagIdsTextArr2Attr($value, $data)
    {
        $value = $value ? $value : (isset($data['house_tag_ids']) ? $data['house_tag_ids'] : '');
        $tag_name_arr = [];
        if ($value) {
            // $tag_name_arr=HouseTag::where('status','normal')->where('id','in',$value)->orderRaw('is_left_top desc,weigh desc,id desc')->column('name');
            $data = HouseTag::where('status', 'normal')->where('id', 'in', $value)->orderRaw('is_left_top desc,weigh desc,id desc')->select();
            foreach ($data as $k => $v) {
                if (!($k == 0 && $v['is_left_top'] == 2)) {
                    $tag_name_arr[] = $v['name'];
                }
            }
        }
        return $tag_name_arr;
    }
    public function getIsHotList()
    {
        return [1 => __('否'), 2 => __('是')];
    }
    // 售卖情况:1=在售,2=售罄,3=待售
    public function getSaleStateList()
    {
        return ['1' => __('在售'), '2' => __('售罄'), '3' => __('待售')];
    }

    public function getIsLowPriceProtectList()
    {
        return ['1' => __('Is_low_price_protect 1'), '2' => __('Is_low_price_protect 2')];
    }

    public function getStatusList()
    {
        return ['normal' => __('上架'), 'hidden' => __('下架')];
    }

    public function getCheckStatusList()
    {
        return ['0' => __('待审核'), '1' => __('已过审'), '2' => __('未过审')];
    }

    public function getSaleStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sale_state']) ? $data['sale_state'] : '');
        $list = $this->getSaleStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getIsLowPriceProtectTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_low_price_protect']) ? $data['is_low_price_protect'] : '');
        $list = $this->getIsLowPriceProtectList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getOpentimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['opentime']) ? $data['opentime'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }

    public function getDeliverytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['deliverytime']) ? $data['deliverytime'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }

    public function getViewtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['viewtime']) ? $data['viewtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getSaletimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['saletime']) ? $data['saletime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getCollecttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['collecttime']) ? $data['collecttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getRecievetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['recievetime']) ? $data['recievetime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setOpentimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDeliverytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setViewtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setSaletimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCollecttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setRecievetimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function housetype()
    {
        return $this->belongsTo('HouseType', 'house_type_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function housedeveloper()
    {
        return $this->belongsTo('HouseDeveloper', 'house_developer_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    
    public function getRenderingMapAttr($value, $data)
    {
        $value = $value ? $value : '';
        return cdnurl($value,true);
    }
    
    public function getAreaMapAttr($value, $data)
    {
        $value = $value ? $value : '';
        return cdnurl($value,true);
    }
    
    public function getBuildImageAttr($value, $data)
    {
        $value = $value ? $value : '';
        return cdnurl($value,true);
    }
    
    public function getVrUrlAttr($value, $data)
    {
        $value = $value ? $value : '';
        return cdnurl($value,true);
    }
    
    public function getHouseTypeImageAttr($value, $data)
    {
        $value = $value ? $value : '';
        return cdnurl($value,true);
    }
}
